#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "mygrafx.h"
#include <iostream>

using namespace std;

MyGrafx::MyGrafx()
{
}

MyGrafx::MyGrafx( const MyGrafx &new_obj )
{
     //cout << "copy constructor\n";
    SCREEN_WIDTH = new_obj.SCREEN_WIDTH; //= 800;
    SCREEN_HEIGHT = new_obj.SCREEN_HEIGHT; //= 600;
    SCREEN_BPP = new_obj.SCREEN_BPP; //= 32;

//    if( NULL != new_obj.GlobalScreen::screen )
  //  {
        //GlobalScreen::screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
        picture = NULL;
    //}
    if( NULL != new_obj.picture )
        picture = SDL_ConvertSurface(new_obj.picture, new_obj.picture->format, SDL_SWSURFACE);

    pictureLocation = new_obj.pictureLocation;

}

MyGrafx::~MyGrafx()
{
    if( picture != NULL )
        SDL_FreeSurface( picture );
//    else
//        cout << "picture is nULL\n";

}

void MyGrafx::operator=( const MyGrafx &new_obj )
{
    //cout << "operator=\n";
    SCREEN_WIDTH = new_obj.SCREEN_WIDTH; //= 800;
    SCREEN_HEIGHT = new_obj.SCREEN_HEIGHT; //= 600;
    SCREEN_BPP = new_obj.SCREEN_BPP; //= 32;

  //  if( NULL != new_obj.GlobalScreen::screen )
  //  {
       // GlobalScreen::screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
        picture = NULL;
  //  }
    if( NULL != new_obj.picture )
        picture = SDL_ConvertSurface(new_obj.picture, new_obj.picture->format, SDL_SWSURFACE);

    pictureLocation = new_obj.pictureLocation;

}


// used only when creating a SDL display surfaces
void MyGrafx::setup(char* windowcaption)
{
    picture = NULL;

    //Screen attributes
    SCREEN_WIDTH = 800;
    SCREEN_HEIGHT = 500;
    SCREEN_BPP = 16;

    //Set up the screen
    GlobalScreen::screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    // screen is deleted by SDL_Quit

    SDL_WM_SetCaption( windowcaption, NULL );
}


void MyGrafx::load(string image_file)
{
    picture = NULL;
    SDL_Surface *unopt_picture = NULL;

	unopt_picture = IMG_Load(image_file.c_str());

	if (unopt_picture == NULL) {
		fprintf(stderr, "Couldn't load %s: %s\n", "Image", SDL_GetError());
	}

    picture = SDL_DisplayFormatAlpha( unopt_picture );
	SDL_FreeSurface( unopt_picture );
}

void MyGrafx::draw( int x, int y, SDL_Surface* screen )
{
    pictureLocation.x = x;
	pictureLocation.y = y;

    SDL_BlitSurface(picture, NULL, screen, &pictureLocation);
}

void MyGrafx::drawAnim(int screenLocationX,   int screenLocationY,
                      int spriteSizeW,        int spriteSizeH,
                      int spritemapLocationX, int spritemapLocationY,
                      SDL_Surface* screen)
{
    //SDL_rect dest;
    pictureLocation.x = screenLocationX;
    pictureLocation.y = screenLocationY;

    // sprite location on sprite map
    SDL_Rect spriteLocation;
    spriteLocation.x = spritemapLocationX;
    spriteLocation.y = spritemapLocationY;
    spriteLocation.w = spriteSizeW;
    spriteLocation.h = spriteSizeH;

    SDL_BlitSurface(picture, &spriteLocation, screen, &pictureLocation);
}

void MyGrafx::flip(SDL_Surface* screen)
{
    SDL_Flip(screen);
}

void MyGrafx::clear(SDL_Surface* screen)
{
    SDL_FillRect(screen, NULL, 0x00000000);
}
